<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="车辆id" prop="truckId">
      <el-input v-model="dataForm.truckId" placeholder="车辆id"></el-input>
    </el-form-item>
    <el-form-item label="发动机编号" prop="engineNumber">
      <el-input v-model="dataForm.engineNumber" placeholder="发动机编号"></el-input>
    </el-form-item>
    <el-form-item label="注册时间" prop="registrationDate">
      <el-input v-model="dataForm.registrationDate" placeholder="注册时间"></el-input>
    </el-form-item>
    <el-form-item label="国家强制报废日期" prop="mandatoryScrap">
      <el-input v-model="dataForm.mandatoryScrap" placeholder="国家强制报废日期"></el-input>
    </el-form-item>
    <el-form-item label="检验有效期" prop="expirationDate">
      <el-input v-model="dataForm.expirationDate" placeholder="检验有效期"></el-input>
    </el-form-item>
    <el-form-item label="整备质量" prop="overallQuality">
      <el-input v-model="dataForm.overallQuality" placeholder="整备质量"></el-input>
    </el-form-item>
    <el-form-item label="核定载质量" prop="allowableWeight">
      <el-input v-model="dataForm.allowableWeight" placeholder="核定载质量"></el-input>
    </el-form-item>
    <el-form-item label="外廓尺寸" prop="outsideDimensions">
      <el-input v-model="dataForm.outsideDimensions" placeholder="外廓尺寸"></el-input>
    </el-form-item>
    <el-form-item label="行驶证有效期" prop="validityPeriod">
      <el-input v-model="dataForm.validityPeriod" placeholder="行驶证有效期"></el-input>
    </el-form-item>
    <el-form-item label="道路运输证号" prop="transportCertificateNumber">
      <el-input v-model="dataForm.transportCertificateNumber" placeholder="道路运输证号"></el-input>
    </el-form-item>
    <el-form-item label="图片信息" prop="picture">
      <el-input v-model="dataForm.picture" placeholder="图片信息"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          truckId: '',
          engineNumber: '',
          registrationDate: '',
          mandatoryScrap: '',
          expirationDate: '',
          overallQuality: '',
          allowableWeight: '',
          outsideDimensions: '',
          validityPeriod: '',
          transportCertificateNumber: '',
          picture: ''
        },
        dataRule: {
          truckId: [
            { required: true, message: '车辆id不能为空', trigger: 'blur' }
          ],
          engineNumber: [
            { required: true, message: '发动机编号不能为空', trigger: 'blur' }
          ],
          registrationDate: [
            { required: true, message: '注册时间不能为空', trigger: 'blur' }
          ],
          mandatoryScrap: [
            { required: true, message: '国家强制报废日期不能为空', trigger: 'blur' }
          ],
          expirationDate: [
            { required: true, message: '检验有效期不能为空', trigger: 'blur' }
          ],
          overallQuality: [
            { required: true, message: '整备质量不能为空', trigger: 'blur' }
          ],
          allowableWeight: [
            { required: true, message: '核定载质量不能为空', trigger: 'blur' }
          ],
          outsideDimensions: [
            { required: true, message: '外廓尺寸不能为空', trigger: 'blur' }
          ],
          validityPeriod: [
            { required: true, message: '行驶证有效期不能为空', trigger: 'blur' }
          ],
          transportCertificateNumber: [
            { required: true, message: '道路运输证号不能为空', trigger: 'blur' }
          ],
          picture: [
            { required: true, message: '图片信息不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/base/trucklicense/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.truckId = data.truckLicense.truckId
                this.dataForm.engineNumber = data.truckLicense.engineNumber
                this.dataForm.registrationDate = data.truckLicense.registrationDate
                this.dataForm.mandatoryScrap = data.truckLicense.mandatoryScrap
                this.dataForm.expirationDate = data.truckLicense.expirationDate
                this.dataForm.overallQuality = data.truckLicense.overallQuality
                this.dataForm.allowableWeight = data.truckLicense.allowableWeight
                this.dataForm.outsideDimensions = data.truckLicense.outsideDimensions
                this.dataForm.validityPeriod = data.truckLicense.validityPeriod
                this.dataForm.transportCertificateNumber = data.truckLicense.transportCertificateNumber
                this.dataForm.picture = data.truckLicense.picture
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/base/trucklicense/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'truckId': this.dataForm.truckId,
                'engineNumber': this.dataForm.engineNumber,
                'registrationDate': this.dataForm.registrationDate,
                'mandatoryScrap': this.dataForm.mandatoryScrap,
                'expirationDate': this.dataForm.expirationDate,
                'overallQuality': this.dataForm.overallQuality,
                'allowableWeight': this.dataForm.allowableWeight,
                'outsideDimensions': this.dataForm.outsideDimensions,
                'validityPeriod': this.dataForm.validityPeriod,
                'transportCertificateNumber': this.dataForm.transportCertificateNumber,
                'picture': this.dataForm.picture
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
